package org.jeecg.modules.appuser.job;

import cn.hutool.core.date.DateUtil;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.appuser.constant.PayTypeConstant;
import org.jeecg.modules.appuser.entity.TbUserGameBuiding;
import org.jeecg.modules.appuser.entity.TbUserSqt;
import org.jeecg.modules.appuser.entity.TbUserSqtDetail;
import org.jeecg.modules.appuser.entity.TbUserUpgradeConfig;
import org.jeecg.modules.appuser.service.ITbUserGameBuidingService;
import org.jeecg.modules.appuser.service.ITbUserSqtDetailService;
import org.jeecg.modules.appuser.service.ITbUserSqtService;
import org.jeecg.modules.appuser.service.ITbUserUpgradeConfigService;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.jeecg.modules.system.service.ISysDictService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 盖楼结算  每 1分钟 结算一次
 */
public class SettleGameBuildingTask implements Job {

    @Autowired
    private ITbUserGameBuidingService tbUserGameBuidingService;

    @Autowired
    private ITbUserUpgradeConfigService tbUserUpgradeConfigService;

    @Autowired
    private ITbUserSqtDetailService tbUserSqtDetailService;

    @Autowired
    private ITbUserSqtService tbUserSqtService;

    @Autowired
    private ITbMnUserService tbMnUserService;

    @Autowired
    private ISysDictService sysDictService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<TbUserGameBuiding> tbUserGameBuidingList = tbUserGameBuidingService.list();
        if (null != tbUserGameBuidingList && tbUserGameBuidingList.size() > 0) {
            for (TbUserGameBuiding tbUserGameBuiding : tbUserGameBuidingList) {
                String gradeId = tbUserGameBuiding.getGrade();
                //得到升级规则
                TbUserUpgradeConfig tbUserUpgradeConfig = tbUserUpgradeConfigService.getById(gradeId);
                if (null != tbUserUpgradeConfig) {
                    String businessProportion = tbUserUpgradeConfig.getBusinessProportion();
                    BigDecimal bizProportion_Dec = null;
                    String unbusinessProportion = tbUserUpgradeConfig.getUnbusinessProportion();

                    //产币速率
                    String productCoinRate = tbUserUpgradeConfig.getProductCoinRate();
                    BigDecimal productCoinRateDec = new BigDecimal(productCoinRate);
                    int businessHours = tbUserGameBuiding.getBusinessHours();
                    BigDecimal sourceRateDec = new BigDecimal(1);
                    if (businessHours > 0) {
                        bizProportion_Dec = new BigDecimal(businessProportion);
                        int totalConsumeHours = tbUserGameBuiding.getTotalConsumeHours();
                        if (totalConsumeHours > 0) {
                            businessHours = businessHours - 1;
                            tbUserGameBuiding.setBusinessHours(businessHours);
                            totalConsumeHours = totalConsumeHours - 1;
                            tbUserGameBuiding.setTotalConsumeHours(totalConsumeHours);
                        }
                    } else {
                        bizProportion_Dec = new BigDecimal(unbusinessProportion);
                    }

                    TbMnUser tbMnUser= tbMnUserService.getById(tbUserGameBuiding.getUserId());
                    if(null==tbMnUser||null==tbMnUser.getIntegralAgent()){
                        continue;
                    }
                    Integer  integralAgent= tbMnUser.getIntegralAgent();
                    BigDecimal rewardSQTValueDec =new BigDecimal("0");
                    BigDecimal  integralAgent_magnification=new BigDecimal("1");
                    if(integralAgent==0){
                        //奖励的SQT
                          rewardSQTValueDec = bizProportion_Dec.add(productCoinRateDec);
                    }else{

                        List<DictModel> dictModelList = sysDictService.queryDictItemsByCode("agent_product_coin");
                        if (null != dictModelList && dictModelList.size() > 0) {
                            for (DictModel dictModel : dictModelList) {
                                String text=dictModel.getText().trim();
                                if(text.equals(String.valueOf(integralAgent))){
                                    integralAgent_magnification=new BigDecimal(dictModel.getValue());
                                }
                            }
                        }
                        bizProportion_Dec= bizProportion_Dec.multiply(integralAgent_magnification);
                        rewardSQTValueDec = bizProportion_Dec.add(productCoinRateDec);
                    }
                    //判断是否是代理，并且修改奖励值
                    TbUserSqt tbUserSqt = tbUserSqtService.getOneByUserId(tbUserGameBuiding.getUserId());

                    if (null!=tbUserSqt) {
                        synchronized (tbUserSqt) {
                            BigDecimal balanceDec = tbUserSqt.getBalance();
                            BigDecimal new_balance = balanceDec.add(rewardSQTValueDec);
                            tbUserSqt.setBalance(new_balance);
                            if (tbUserSqtService.updateById(tbUserSqt)) {
                                saveSqtDetail(tbUserSqt, new_balance, rewardSQTValueDec);
                                tbUserGameBuidingService.updateById(tbUserGameBuiding);
                            }
                        }
                    }
                }
        }
    }

}

    private void saveSqtDetail(TbUserSqt tbUserSqt, BigDecimal newBalance, BigDecimal rewardSQTValueDec) {
        TbUserSqtDetail tbUserSqtDetail = new TbUserSqtDetail();
        tbUserSqtDetail.setPayFrom(PayTypeConstant.PAYFORM_PRODUCT_COIN);
        tbUserSqtDetail.setPayType(PayTypeConstant.PAYTYPE_INCOME);
        tbUserSqtDetail.setPayNumber(rewardSQTValueDec);
        tbUserSqtDetail.setSqtId(tbUserSqt.getId());
        tbUserSqtDetail.setUserId(tbUserSqt.getUserId());
        tbUserSqtDetailService.save(tbUserSqtDetail);
    }
}
